﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;

namespace SimpleTaskManager.WebUI.HtmlHelpers
{
    /// <summary>
    /// Class containing DropDown HTML extensions
    /// </summary>
    public static class HtmlDropDownExtensions
    {
        /// <summary>
        /// Renders html select list 
        /// </summary>
        /// <typeparam name="TEnum">type of enumeration for which the select list should be created</typeparam>
        /// <param name="htmlHelper">no parameter, indicates that this is extenstion method of HtmlHelper class</param>
        /// <param name="name">name of the form field</param>
        /// <param name="selectedValue">chosen value of the select list</param>
        /// <returns>html select list</returns>
        public static MvcHtmlString EnumDropDownList<TEnum>(this HtmlHelper htmlHelper, string name, TEnum selectedValue)
        {
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();

            IEnumerable<SelectListItem> items = from value in values
                                                select new SelectListItem
                                                {
                                                    Text = value.ToString(),
                                                    Value = value.ToString(),
                                                    Selected = (value.Equals(selectedValue))
                                                };
            return htmlHelper.DropDownList(name, items);
        }

        /// <summary>
        /// Renders html select list for the specified model using given lamba expression
        /// </summary>
        /// <typeparam name="TModel">indicates model for which the helper method is created</typeparam>
        /// <typeparam name="TEnum">type of enumeration for which the select list should be created</typeparam>
        /// <param name="htmlHelper">no parameter, indicates that this is extenstion method of HtmlHelper class</param>
        /// <param name="expression">lamba expression</param>
        /// <returns>HTML select list</returns>
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();

            IEnumerable<SelectListItem> items =
                values.Select(value => new SelectListItem
                {
                    Text = value.ToString(),
                    Value = value.ToString(),
                    Selected = value.Equals(metadata.Model)
                });

            return htmlHelper.DropDownListFor(
                expression,
                items
                );
        }
    }
}